package pixelwaks.gui.impl;

import java.awt.*;

import javax.swing.*;

import common.gui.IViewable;

import pixelwaks.core.impl.Game;
import pixelwaks.core.impl.Player;
import pixelwaks.ctrl.ControlFactory;

/**
 * Class for drawing a life bar for a Player in a PlayerInfoPanel. 
 * The life bar displays the Player's current status (life), and also indicates whether or not 
 * the Player is currently the active Player in two-player games.
 * The life bar is updated with the current status upon calling updateView().
 * @author Joakim
 *
 */
public class LifeBar extends JComponent implements IViewable {
	
	private static final long serialVersionUID = 1L;
	private String playerName;
	private Player player;
	private Boolean currentPlayer;
	private Font arial12;
	private Font arialBold12;
	private int lifeBarWidth;
	private int lifeBarHeight;
	private int xPos;
	private int yPos;
	private int width;
	private int height;

	/**
	 * Creates a lifebar and draws it.
	 * @param player the Player whose status (life) is to be displayed
	 */
	public LifeBar(Player player) {
		
		this.player = player;
		lifeBarWidth = 206;
		lifeBarHeight = 23;
		xPos = 10;
		yPos = 10;
		width = lifeBarWidth + xPos*2;
		height = lifeBarHeight + yPos*2;
		
		setPreferredSize(new Dimension(width, height));
		setVisible(true);
		
		ControlFactory.getObserver(ControlFactory.PLAYER_CTRL).addView(this);
		
		//fonts
		arial12 = new Font("Arial", Font.PLAIN, 12);
		arialBold12 = new Font("Arial", Font.BOLD, 12);
		playerName = player.getName();
	}

	@Override
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		if(Game.getInstance().getCurrentPlayer() == player)
			currentPlayer = true;
		else
			currentPlayer = false;
		
		double playerStatus = player.getStatus();
		
		g2.setColor(Color.BLACK);
		if(currentPlayer)
			g2.fillRect(xPos, yPos, lifeBarWidth, lifeBarHeight);
		else
			g2.fillRect(xPos+1, yPos+1, lifeBarWidth-2, lifeBarHeight-2);
		
		if(playerStatus > 0) {
			g2.setColor(Color.LIGHT_GRAY);
			g2.fillRect(xPos+3, yPos+3, lifeBarWidth-6, lifeBarHeight-6);
			//Color of life bar changes (green -> yellow -> red) depending on energy left
			if(playerStatus > 66)
				g2.setColor(Color.GREEN);
			else if(playerStatus > 33 && playerStatus < 67)
				g2.setColor(Color.ORANGE);
			else
				g2.setColor(Color.RED);
			
			//Round up when playerHealth < 100
			if(playerStatus < 100)
				g2.fillRect(xPos+3, yPos+3, (int) playerStatus*2 + 1, lifeBarHeight-6);
			else
				g2.fillRect(xPos+3, yPos+3, (int) playerStatus*2, lifeBarHeight-6);
			
			g2.setColor(Color.BLACK);
			if(currentPlayer)
				g2.setFont(arialBold12);
			else
				g2.setFont(arial12);
			g2.drawString(playerName, xPos+7, height/2 + g2.getFontMetrics().getHeight()/3);
			
			String health;
			//Round up when playerHealth < 100
			if(playerStatus < 100) {
				health = (int)playerStatus + 1 + "%";
				g2.drawString(health, xPos+lifeBarWidth - g2.getFontMetrics().stringWidth(health) - 6, height/2 + g2.getFontMetrics().getHeight()/3);
			}
			else {
				health = (int)playerStatus + "%";
				g2.drawString(health, xPos+lifeBarWidth - g2.getFontMetrics().stringWidth(health) - 6, height/2 + g2.getFontMetrics().getHeight()/3);
			}
		}
		else {
			g2.setColor(Color.RED);
			g2.setFont(arialBold12);
			g2.drawString(playerName + " is dead!", xPos+7, height/2 + g2.getFontMetrics().getHeight()/3);
		}	
	}

	@Override
	public void updateView(Object o) {
		repaint();
	}
}
